package cn.zcx;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * description:
 * author:zcx
 * lenovo
 * 时间：2022.02.07.16.53
 */
public class T11 {
    public static void main(String[] args) {
        var t = new TT();
        new Thread(() -> {
            System.out.println("工作线程开始，增加元素");
            for (int i = 0; i < 20; i++) {
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                t.add();
            }
        }, "T1").start();
        new Thread(() -> {
            System.out.println("监控线程开始");
            while (!t.count()) ;
            System.out.println("集合元素到5个了....");
        }, "T2").start();
    }
}
class TT{
    volatile List<String> list=new ArrayList<>();
    //List<String> list=new ArrayList<>();
    public void add(){
        String item="item"+list.size();
        list.add(item);
        System.out.println(item);
    }
    public boolean count(){
       return list.size()>=5;

    }
}
